package cn.bugstack.domain.strategy.service.rule.tree.factory.engine.impl;

import cn.bugstack.domain.strategy.model.vo.RuleLogicCheckTypeVo;
import cn.bugstack.domain.strategy.model.vo.RuleTreeNodeLineVo;
import cn.bugstack.domain.strategy.model.vo.RuleTreeNodeVo;
import cn.bugstack.domain.strategy.model.vo.RuleTreeVo;
import cn.bugstack.domain.strategy.service.rule.tree.ILogicTreeNode;
import cn.bugstack.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import cn.bugstack.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 19872
 * 决策树引擎
 */
@Slf4j
public class DecisionTreeEngine implements IDecisionTreeEngine {

    private final Map<String, ILogicTreeNode> logicTreeNodeGroup;

    private final RuleTreeVo ruleTreeVo;

    public DecisionTreeEngine(Map<String, ILogicTreeNode> logicTreeNodeGroup,RuleTreeVo ruleTreeVo){
        this.logicTreeNodeGroup = logicTreeNodeGroup;
        this.ruleTreeVo = ruleTreeVo;
    }



    @Override
    public DefaultTreeFactory.StrategyAwardVO process(String userId, Long strategyId, Integer awardId, Date endDateTime) {
        DefaultTreeFactory.StrategyAwardVO strategyAwardData = null;
        // 获取基础信息
        String nextNode = ruleTreeVo.getTreeRootRuleNode();
        // 获取基础信息和节点的映射map
        Map<String, RuleTreeNodeVo> treeNodeMap = ruleTreeVo.getTreeNodeMap();
        // 通过map拿到树节点
        RuleTreeNodeVo ruleTreeNode = treeNodeMap.get(nextNode);

        while(null!=ruleTreeNode){
            // 通过树节点的ruleKey，获取对应的规则节点
            ILogicTreeNode logicTreeNode = logicTreeNodeGroup.get(ruleTreeNode.getRuleKey());
            String ruleValue = ruleTreeNode.getRuleValue();
            DefaultTreeFactory.TreeActionEntity logicEntity = logicTreeNode.logic(userId, strategyId, awardId,ruleValue,endDateTime);

            RuleLogicCheckTypeVo ruleLogicCheckTypeVo = logicEntity.getRuleLogicCheckType();
            strategyAwardData = logicEntity.getStrategyAwardVO();
            log.info("决策树引擎【{}】 treeId：{} node：{}  code：{}",ruleTreeVo.getTreeName(),ruleTreeVo.getTreeId(),nextNode,ruleLogicCheckTypeVo.getCode());
            nextNode = nextNode(ruleLogicCheckTypeVo.getCode(),ruleTreeNode.getTreeNodeLineVoList());
            ruleTreeNode = treeNodeMap.get(nextNode);

        }
        // 返回最终结果
        return strategyAwardData;

    }

    private String nextNode(String matterValue, List<RuleTreeNodeLineVo> ruleTreeNodeLineVoList){
        // 找到根节点
        if(null==ruleTreeNodeLineVoList || ruleTreeNodeLineVoList.isEmpty()){
            return null;
        }
        for (RuleTreeNodeLineVo nodeLineVo : ruleTreeNodeLineVoList) {
            if (decisionLogic(matterValue,nodeLineVo)){
                return nodeLineVo.getRuleNodeTo();
            }
        }
        return null;
    }

    public boolean decisionLogic(String matterValue, RuleTreeNodeLineVo nodeLine){
        switch (nodeLine.getRuleLimitType()){
            case EQUAL:
                return matterValue.equals(nodeLine.getRuleLimitValue().getCode());
            case  GT:
            case  LT:
            case  GE:
            case  LE:
            default:
                return false;
        }
    }

}
